package com.example.controller;

import com.example.common.Result;
import com.example.entity.Clockingtime;
import com.example.service.ClockingtimeService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 用户打卡时间前端接口
 */
@RestController
@RequestMapping("/clockingtime")
public class ClockingtimeController {

    @Resource
    private ClockingtimeService clockingtimeService;

    /**
     * 新增
     */
    @PostMapping("/add")
    public Result add(@RequestBody Clockingtime clockingtime) {
        clockingtimeService.add(clockingtime);
        return Result.success();
    }

    @PostMapping("/addById")
    public Result addById(@RequestBody Clockingtime clockingtime) {
        Clockingtime existingClockingtime = clockingtimeService.selectByUid(clockingtime);

        if (existingClockingtime == null) {
            clockingtime.setTime(String.valueOf(15));
            if(clockingtime.getHaventime() == null){
                clockingtime.setHaventime((float) 0);
            }
            clockingtimeService.add(clockingtime);
        } else {
            Float existingCount = existingClockingtime.getHaventime();

            // 进行累加操作
            Float newCount = existingCount + clockingtime.getHaventime();

            // 将结果转换为字符串并设置回现有数据中
            existingClockingtime.setHaventime(newCount);
            clockingtimeService.updateById(existingClockingtime); // 使用 existingClockingtime 而不是 clockingtime
        }

        return Result.success();
    }


    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable Integer id) {
        clockingtimeService.deleteById(id);
        return Result.success();
    }

    /**
     * 批量删除
     */
    @DeleteMapping("/delete/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        clockingtimeService.deleteBatch(ids);
        return Result.success();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public Result updateById(@RequestBody Clockingtime clockingtime) {
        clockingtimeService.updateById(clockingtime);
        return Result.success();
    }

    /**
     * 根据ID查询
     */
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id) {
        Clockingtime clockingtime = clockingtimeService.selectById(id);
        return Result.success(clockingtime);
    }

    /**
     * 查询所有
     */
    @GetMapping("/selectAll")
    public Result selectAll(Clockingtime clockingtime ) {
        List<Clockingtime> clockingtimeList= clockingtimeService.selectAll(clockingtime);
        return Result.success(clockingtimeList);
    }

    /**
     * 分页查询
     */
    @GetMapping("/selectPage")
    public Result selectPage(Clockingtime clockingtime,
                             @RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<Clockingtime> pageInfo = clockingtimeService.selectPage(clockingtime, pageNum, pageSize);
        return Result.success(pageInfo);
    }
}
